package com.example.springboot.scheduled;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 测试根据接口创建定时任务
 * <p> 模拟接口，根据接口添加任务或者删除任务
 */

@Component
public class TestApiScheduleManual implements ApplicationRunner {

    @Autowired
    private ScheduleTaskManual taskManual;

    /**
     * 模拟接口，根据接口添加任务或者删除任务
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String taskName = "add-task";
        //添加
        taskManual.addTask(taskName, "0/5 * * * * ?");

        //暂停1分钟后，结束任务
        try {
            TimeUnit.MINUTES.sleep(1);
            taskManual.removeTask(taskName);
        }catch (InterruptedException e){
            throw  e;
        }


    }
}
